package com.application.room.pojo;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.application.actor.pojo.Actor;

public class RoomStatus {
	int maxSize;
	static byte STATUS_READY = 1;
	static byte STATUS_ABORT = 0;
	ArrayList<Integer> uids = new ArrayList<Integer>();
	ArrayList<String> names = new ArrayList<String>();
	ArrayList<Byte> sids = new ArrayList<Byte>();
	ArrayList<Byte> status = new ArrayList<Byte>();
	ArrayList<String> icons = new ArrayList<String>();
	public Map<Integer, Actor> actors = new ConcurrentHashMap<Integer, Actor>();
	public int minStartUser = 1;

	public RoomStatus(int size) {
		maxSize = size;

	}

	public int getUserid(int id) {
		return uids.get(id);
	}

	public int getSeatid(int id) {
		return sids.get(id);
	}

	private boolean addUser(Actor a, String uname, byte sid, byte sta, String icon) {
		
		int idx = uids.indexOf(a.getId());
		if (idx < 0) {
			uids.add(a.getId());
			names.add(uname);
			sids.add(sid);
			status.add(sta);
			icons.add(icon);
		} else {
			uids.set(idx, a.getId());
			sids.set(idx, sid);
			names.set(idx, uname);
			status.set(idx, sta);
			icons.set(idx, icon);
		}
		actors.put(a.getId(), a);
		return true;
	}

	private boolean removeUser(int uid) {
		int idx = uids.indexOf(uid);
		if (idx >= 0) {
			uids.remove(idx);
			sids.remove(idx);
			status.remove(idx);
			names.remove(idx);
			icons.remove(idx);
			actors.remove(uid);
			return true;
		}
		return false;
	}

	public void reset() {
		for (int i = 0; i < status.size(); i++)
			status.set(i, STATUS_ABORT);
	}

	public byte getUserSeatId(int uid) {
		int idx = uids.indexOf(uid);
		if (idx < 0)
			return -1;
		return sids.get(idx);
	}

	public int getUserStatus(int uid) {
		int idx = uids.indexOf(uid);
		if (idx < 0)
			return -1;
		return status.get(idx);
	}

	public boolean setUserStatus(int uid, byte sta) {
		int idx = uids.indexOf(uid);
		if (idx < 0)
			return false;
		status.set(idx, sta);
		return true;
	}

	public boolean userChangeSeat(int uid, byte newsid) {
		int idx = uids.indexOf(uid);
		if (idx < 0)
			return false;
		if (getUserStatus(uid) == STATUS_READY)
			return false;
		if (!seatIsFree(newsid))
			return false;
		sids.set(idx, newsid);
		return true;
	}

	public boolean userOut(int uid) {
		return removeUser(uid);
	}

	public boolean userIn(Actor  a, String uname, String icon) {
		if (uids.contains(a.getId()))
			return true;
	 
		if (isFull())
			return false;
		byte sid = getUseableSid();
		if (sid < 0)
			return false;
		return addUser(a, uname, sid, STATUS_ABORT, icon);
	}

	public boolean userReady(int uid) {
		if (getUserStatus(uid) == STATUS_READY)
			return false;
		return setUserStatus(uid, STATUS_READY);

	}

	public boolean userAbort(int uid) {
		if (getUserStatus(uid) == STATUS_ABORT)
			return false;
		return setUserStatus(uid, STATUS_ABORT);
	}

	private boolean seatIsFree(byte sid) {
		if (sids.contains(sid)) {
			return false;
		}
		return true;
	}

	private byte getUseableSid() {
		for (byte i = 0; i < maxSize; i++) {
			if (!sids.contains(i)) {
				return (byte)i;
			}
		}
		return -1;
	}

	public boolean isFull() {
		if (this.uids.size() >= this.maxSize)
			return true;
		return false;
	}

	public ArrayList getUids() {
		return uids;
	}

	public ArrayList getSids() {
		return sids;
	}

	public ArrayList getStatus() {
		return status;
	}

	public ArrayList getIcons() {
		return icons;
	}

	public int getUserNumber() {
		return this.uids.size();
	}

	public boolean roomIsReady(int mode) {
		boolean teamIsZero = false;
		if (uids.size() < minStartUser) {
			return false;
		}
		if (mode == 1) {// 组队
			teamIsZero = true;
			for (byte i = 0; i < 4; i++) {
				if (!seatIsFree(i)) {
					teamIsZero = false;
					break;
				}
			}
			if (teamIsZero)
				return false;
			teamIsZero = true;
			for (byte i = 4; i < 8; i++) {
				if (!seatIsFree(i)) {
					teamIsZero = false;
					break;
				}
			}
			if (teamIsZero)
				return false;
		}
		if (this.status.contains(STATUS_ABORT)) {
			return false;
		}
		return true;
	}

	public boolean roomIsEmpty() {
		if (uids.size() == 0) {
			return true;
		}
		return false;
	}
}
